/**
 * JBoot后台框架 v1.0.0
 * Copyright (c) 2017, Chen Peng All Rights Reserved.
 * <p>
 * 更改历史：
 * 2017-05-25 16:44    吴昭毅    创建
 */

package com.simon.nvwo.modules.phonebelong;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.simon.nvwo.common.kit.HttpUtils;
import com.simon.nvwo.common.kit.StringUtils;
import com.simon.nvwo.common.kit.excel.ExcelLogs;
import com.simon.nvwo.common.kit.excel.ExcelUtil;
import com.simon.nvwo.modules.phonebelong.dto.DTO;
import com.simon.nvwo.modules.phonebelong.entity.Mobile;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;


import java.io.*;
import java.util.*;

/**
 * TODO 添加说明
 *
 * @author 吴昭毅
 * @version 2017-05-25 16:44
 */
public class MobileBelongService {
    //API域名
    private final static String HOST = "servlet://ali-mobile.showapi.com";
    //APPCODE
    private final static String APP_CODE="18e06bc4f27041d4b5d93aec6c54ba48";

    private final static Logger logger=Logger.getLogger(MobileBelongService .class);


    public static void main(String[] args) {
        String path = "/6-1";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + APP_CODE);
        Map<String, String> querys = new HashMap<String, String>();

        //测试excle写入写出


        InputStream file = null;
        try {
            //格式 number phone province  city phone在excel中要以文本的形式存放（数据-分列修改文本）
            file = new FileInputStream("F:\\123.xls");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        List<Mobile> mobiles=(List<Mobile>) ExcelUtil.importExcel(Mobile.class,file , "yyyy/MM/dd HH:mm:ss", new ExcelLogs(),0);

        Iterator<Mobile> iterator=mobiles.iterator();

        List<Mobile> list=new ArrayList<Mobile>();


        while(iterator.hasNext()){

            Mobile tempmobile=iterator.next();
            //phone在excel中要以文本的形式存放（数据-分列修改文本）
            String phone=tempmobile.getPhone()==null?"":tempmobile.getPhone();
            String name=tempmobile.getName()==null?"":tempmobile.getName();

            querys.put("num", phone);
            try {

                HttpResponse response = HttpUtils.doGet(HOST, path, method, headers, querys);
                //获取response的body
                Mobile mobile=new Mobile();

                //转化json为Mobile对象
                JSONObject temp=new JSONObject();
                temp= JSON.parseObject(EntityUtils.toString(response.getEntity()));

                //null判断和处理
                if(StringUtils.notNull(temp.get("showapi_res_body")==null?"": temp.get("showapi_res_body").toString()) ){

                    //解析Json数组
                    JSONObject json=(JSONObject) temp.get("showapi_res_body");

                    DTO dto=JSON.toJavaObject(json, DTO.class);
                    //0为成功，其他失败。失败时不扣点数
                    String ret_code=dto.getRet_code();

                    if(ret_code !=null && ("0").equals(ret_code)){

                        mobile.setPhone(phone);
                        mobile.setProvince(dto.getProv());
                        mobile.setCity(dto.getCity());
                    }else{
                        mobile.setPhone(phone);
                        mobile.setProvince("0");
                        mobile.setCity("0");

                    }

                }else{

                    mobile.setPhone(phone);
                    mobile.setProvince("0");
                    mobile.setCity("0");
                }
                mobile.setName(name);
                logger.info(mobile);
                list.add(mobile);

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("phone get no location: "+phone);
            }

            querys.remove("num");
        }

        try {
            String[] excelheaders = {"姓名","手机号","省份","城市"};
            File f=new File("F:\\456.xls");
            OutputStream out =new FileOutputStream(f);

            ExcelUtil.exportExcel(excelheaders, list, out);
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
